package com.framework.Table;

import com.framerock.db.DBConn;
import com.framerock.baselib.CLog;
import com.framerock.baselib.Rock;
import com.framework.Flow.FlowGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 更新和创建数据库模块
 * */
public class TableAll{

    /**
     * 获取需要更新的表格
     * */
    private static List<Table> getAllTable(String num, String group)
    {
        List<Table> lTable  = new ArrayList<Table>();
        Table tab;
        Map<String, String> allGroup = FlowGroup.getAllGroup();
        String grouv, nums[],groups[];int i,j;

        //全部
        if((num.equals("all") || Rock.isEmpt(num)) && Rock.isEmpt(group)){
            for (Map.Entry<String, String> entry : allGroup.entrySet()) {
                grouv = entry.getKey();
                nums  = entry.getValue().split(",");
                for (i = 0; i < nums.length; i++) {
                    tab = Table.get(nums[i], grouv);
                    if(tab != null)lTable.add(tab);
                }
            }
        }else if(!Rock.isEmpt(num)){ //只更新编号
            nums = num.split(",");
            for (i = 0; i < nums.length; i++) {
                tab = Table.get(nums[i], "");
                if(tab != null)lTable.add(tab);
            }
        }else{
            if(!Rock.isEmpt(group)){
                groups = group.split(",");
                String numt;
                for (i = 0; i < groups.length; i++) {
                    numt = Rock.getMapString(allGroup, groups[i]);
                    if(!Rock.isEmpt(numt)) {
                        nums = numt.split(",");
                        for (j = 0; j < nums.length; j++){
                            tab = Table.get(nums[j], groups[i]);
                            if(tab != null)lTable.add(tab);
                        }
                    }
                }
            }
        }

        return lTable;
    }

    /**
     * 创建表
     * num 编号
     * group 组名
     * */
    private static String create(DBConn dbs, String num, String group)
    {
        String str = "",str1="",alltab[] = dbs.getAlltable();
        int i;
        Table tab;
        List<Table> lTable = getAllTable(num, group);
        for(i=0;i<lTable.size();i++){
            tab  = lTable.get(i);
            tab.setDb(dbs);
            str1 = tab.createTable(alltab);
            if(dbs.isError())return "";
            str  = "["+tab.getTablename()+"]createTable("+str1+");";
            CLog.print(str);
        }

        if(!dbs.isError())for(i=0;i<lTable.size();i++){
            tab  = lTable.get(i);
            str1 = tab.createMode();
            if(!Rock.isEmpt(str1)) {
            	CLog.print("["+tab.getTablename()+"]element("+str1+");");
            }
        }
        return "";
    }

    /**
     * 更新所有的表
     * num 编号
     * group 组名
     * */
    private static String update(DBConn dbs, String num, String group)
    {
        String str = "",str1="",alltab[] = dbs.getAlltable();
        int i;
        Table tab;
        List<Table> lTable = getAllTable(num, group);
        for(i=0;i<lTable.size();i++){
            tab  = lTable.get(i);
            tab.setDb(dbs);
            str1 = tab.updateTable(alltab);
            if(dbs.isError())return "";
            str  = "["+tab.getTablename()+"]updateTable("+str1+");";
            CLog.print(str);
        }

        if(!dbs.isError())for(i=0;i<lTable.size();i++){
            tab  = lTable.get(i);
            str1 = tab.createMode();
            if(!Rock.isEmpt(str1)) {
            	CLog.print("["+tab.getTablename()+"]element("+str1+");");
            }
        }
        return "";
    }


    /**
     * 执行运行
     * action 动作
     * nums 模块编号
     * group 模块的组
     * */
    public static void queryTable(DBConn dbs, String action, String num, String group)
    {
        if(Rock.contain(action, "create")){
            TableAll.create(dbs, num, group);
        }else{
            TableAll.update(dbs, num, group);
        }
    }
}
